package cn.edu.zzuli.admin.service.impl;


import cn.edu.zzuli.admin.mapper.UserMapper;
import cn.edu.zzuli.admin.service.UserService;
import cn.edu.zzuli.model.dto.admin.UserInfoDto;
import cn.edu.zzuli.model.entity.User;
import cn.edu.zzuli.model.vo.identity.UserQueryVo;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<User> findAll(Integer page, Integer limit, UserQueryVo userQueryVo) {
        //分页查询
        Page<User> pageCod = new Page<>(page,limit);
        //条件查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        String nickName = userQueryVo.getNickName();
        String gender = userQueryVo.getGender();
        String  status = userQueryVo.getStatus();
        if (!StringUtils.isEmpty(gender)){
            if("男".equals(gender)){
                gender = "0";
                wrapper.eq("gender",gender);
            }else if ("女".equals(gender)){
                gender = "1";
                wrapper.eq("gender",gender);
            }else {
                System.out.println("不规范输入");
            }
        }
        if (!StringUtils.isEmpty(nickName)){
            wrapper.like("nick_name",nickName);
        }
        if(status != null){
            System.out.println(status);
            if("正常".equals(status)){
                status = "0";
                wrapper.eq("locked",status);
            }else if ("锁定".equals(status)){
                status = "1";
                wrapper.eq("locked",status);
            }else{
                System.out.println("不规范输入");
            }

        }
        Page<User> userPage = baseMapper.selectPage(pageCod, wrapper);
        return userPage;
    }

    @Override
    public UserInfoDto getUserInfoById(Integer id) {
        UserInfoDto userInfoDto =  baseMapper.selectOneUserInf(id);
        return userInfoDto;
    }
}
